package com.dq.controller;


import com.dq.mapper.UserMapper;
import com.dq.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/info")
public class InfoController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    JavaMailSenderImpl mailSender;

    @RequestMapping("/contact-us")
    public String contactUs(){
        return "/web1/contact-us";
    }

    @RequestMapping("/about-us")
    public String aboutUs(){
        return "/web1/about-us";
    }

    @RequestMapping("/contactUs")
    public String contactUs(@RequestParam(value = "subject",required = false)String subject,
                            @RequestParam(value = "message",required = false)String message1){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String id = userDetails.getUsername();
        User user = userMapper.getUserById(id);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(message1);

        message.setTo("1548436972@qq.com");
        message.setFrom(user.getEmail());
        mailSender.send(message);
        return "redirect:/";
    }

}
